package com.crm.CLdriving.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.plugins.Page;
import com.crm.CLdriving.common.PageResponse;
import com.crm.CLdriving.dto.PageReq.PageNumber;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.crm.CLdriving.common.BaseResponse;
import com.crm.CLdriving.dto.req.CoachDeleteReqDto;
import com.crm.CLdriving.dto.req.CoachInputReqDto;
import com.crm.CLdriving.dto.req.CoachInsertReqDto;
import com.crm.CLdriving.dto.req.CoachSelOneReqDto;
import com.crm.CLdriving.dto.req.CoachUpdateReqDto;
import com.crm.CLdriving.dto.resp.CoachSelectAllRespDto;
import com.crm.CLdriving.dto.resp.CoachSelectOneRespDto;
import com.crm.CLdriving.enu.ResponseEnum;
import com.crm.CLdriving.mapper.CoachMapper;
import com.crm.CLdriving.po.CoachPO;
import com.crm.CLdriving.service.CoachService;

import lombok.extern.log4j.Log4j2;


@Transactional
@Service
@Log4j2
public class CoachServiceImpl implements CoachService {

	@Value("${data.pageSize.coach}")
	private  int coachSize;
	@Autowired
	private CoachMapper coachMapper;
	
	/**
	 * 通过身份证号判断是否已经录入
	 * 教练信息录入 
	 */
	@Override
	public BaseResponse<?> insert(CoachInsertReqDto coachInsertReqDto) {
		EntityWrapper<CoachPO> wrapper = new EntityWrapper<>();
		wrapper.eq("identity", coachInsertReqDto.getIdentity());
		List<CoachPO> list = coachMapper.selectList(wrapper);
		if (!"[]".equals(list.toString())) {
			log.info("已经录入"+list.toString());
			return BaseResponse.message(ResponseEnum.YJLR);
		}

		CoachPO coachPO = new CoachPO();
		try {
			BeanUtils.copyProperties(coachPO, coachInsertReqDto);
			log.info("copy后："+coachPO.toString());
		} catch (Exception e) {
			log.info("copy:"+e);
		}		
		Integer row = coachMapper.insert(coachPO);
		 if (row == 1) {
			log.info("row____"+row);
			return BaseResponse.message(ResponseEnum.LRCG);
		}
		return BaseResponse.message(ResponseEnum.FAIL);
	}

	/**
	 * 查询 全部教练信息
	 */
	@Override
	public PageResponse<List<CoachSelectAllRespDto>> selectall(PageNumber pageNumber) {
		List<CoachSelectAllRespDto> response = new ArrayList<CoachSelectAllRespDto>();
		Page<CoachPO> page = new Page(pageNumber.getPageNumber(),coachSize);
		List<CoachPO> list = coachMapper.selectPage(page,null);
		//List<CoachPO> list = coachMapper.selectList(null);
		if (!"[]".equals(list.toString())) {
			log.info("查询全部教练信息为："+list.toString());
			try {
				for (CoachPO coachPO : list) {
					CoachSelectAllRespDto coachSelectAllRespDto = new CoachSelectAllRespDto();
					BeanUtils.copyProperties(coachSelectAllRespDto, coachPO);
					response.add(coachSelectAllRespDto);
				}
				log.info("copy后："+response.toString());
			} catch (Exception e) {
				log.info("copy错误："+e);
			}
		}
		return PageResponse.successOfPage(response,page);
	}

	/**
	 * 通过id
	 *查询 某个 教练信息
	 */
	@Override
	public BaseResponse<CoachSelectOneRespDto> selectone(CoachSelOneReqDto coachSelOneReqDto) {
		CoachPO coachPO = coachMapper.selectById(coachSelOneReqDto.getId());
		if (coachPO != null) {
			try {
				CoachSelectOneRespDto response = new CoachSelectOneRespDto();
				BeanUtils.copyProperties(response, coachPO);
				return BaseResponse.successOf(response);
			} catch (Exception e) {
				log.info("copy失败"+e);
			}
		}
		return BaseResponse.message(ResponseEnum.ZWSJ);
	}

	/**
	 * 通过id
	 * 更新教练信息
	 */
	@Override
	public BaseResponse<?> update(CoachUpdateReqDto coachUpdateReqDto) {
		CoachPO coachPO = new CoachPO();
		try {
			BeanUtils.copyProperties(coachPO, coachUpdateReqDto);
			log.info("copy后："+coachPO);
		} catch (Exception e) {
			log.info("copy错误"+e);
		}
		Integer row = coachMapper.updateById(coachPO);
		if (row == 1) {
			log.info("更新教练信息成功");
			return BaseResponse.message(ResponseEnum.GXCG);
		}
		return BaseResponse.message(ResponseEnum.FAIL);
	}

	/**
	 * 通过id
	 * 删除教练信息
	 */
	@Override
	public BaseResponse<?> delete(CoachDeleteReqDto coachDeleteReqDto) {
		Integer row = coachMapper.deleteById(coachDeleteReqDto.getId());
		if (row == 1) {
			log.info("删除教练信息成功");
			return BaseResponse.message(ResponseEnum.SQCG);
		}
		return BaseResponse.message(ResponseEnum.FAIL);
	}

	/**
	 * 筛选框查询
	 */
	@Override
	public BaseResponse<List<CoachPO>> selectInput(CoachInputReqDto coachInputReqDto) {
		EntityWrapper<CoachPO> wrapper = new EntityWrapper<>();
		if (coachInputReqDto.getName() != null && coachInputReqDto.getName() != "") {
			wrapper.like("name", coachInputReqDto.getName());
		}
        if (coachInputReqDto.getIdentity() != null && coachInputReqDto.getIdentity() != "") {
        	wrapper.eq("identity", coachInputReqDto.getIdentity());
		}
		List<CoachPO> list = coachMapper.selectList(wrapper);
		if (CollectionUtils.isNotEmpty(list)) {
			log.info("筛选框查询结果："+list.toString());
			return BaseResponse.successOf(list);
		}		
		return BaseResponse.successOf(null);
	}

}
